package com.example.module.user.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import com.example.common.unit.ConvertUnit;
import com.example.module.user.DO.*;
import com.example.module.user.vo.GetUserByPhoneVO;
import com.example.module.user.vo.UserLoginVO;
import com.example.module.user.form.*;
import com.example.module.user.mapper.UserMapper;
import com.example.module.user.service.UserService;
import com.example.module.user.vo.GetUserVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @author ghy
 * @date 2025/07/31
 * @description 用户实现类
 */
@Service
public class UserImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    /*
     * 用户注册
     *
     * @param userRegisterForm
     * @return void
     * */
    @Override
    public void userRegister(UserRegisterForm userRegisterForm) {
        //判断用户是否存在（根据用户手机号判断）
        String userPhone = userRegisterForm.getUserPhone();
        UserRegisterDO user = userMapper.getUserByPhone(userPhone);
        if (user != null) {
            //用户已存在
            throw new RuntimeException("用户注册失败：该用户已存在");
        }
        //将UserRegisterForm转换为UserRegisterDO并存入数据库
        userMapper.addUser(ConvertUnit.FormToDO(userRegisterForm, UserRegisterDO.class));
    }

    /*
     * 更新用户（根据用户Id查询）
     *
     * @param userUpdateForm
     * @return void
     * */
    @Override
    public void userUpdate(UserUpdateForm userUpdateForm) {
        //从session中获取用户信息
        Object sessionUser = StpUtil.getSession().get("user");
        //将用户信息转换为UserDO类
        UserUpdateDO currentUser = ConvertUnit.DOToDO(sessionUser, UserUpdateDO.class);
        //检查当前用户和传入的用户是否一致
        if (!currentUser.getUserId().equals(userUpdateForm.getUserId())) {
            throw new RuntimeException("用户信息校验失败：待操作用户与当前登录用户身份不匹配");
        }
        userMapper.updateUser(ConvertUnit.FormToDO(userUpdateForm, UserUpdateDO.class));
    }

    /*
     * 删除用户
     *
     * @param userDeleteForm
     * @return void
     * */
    @Override
    public void deleteUser(UserDeleteForm userDeleteForm) {
        //判断用户是否存在（根据用户Id判断）
        Long userId = userDeleteForm.getUserId();
        GetUserByIdDO user = userMapper.getUserById(userId);
        if (user == null) {
            throw new RuntimeException("用户删除失败：用户不存在");
        }
        //删除用户
        userMapper.deleteUser(userId);
    }

    /*
     * 用户登录
     *
     * @param userLoginForm
     * @return UserLoginDO
     * */
    @Override
    public UserLoginVO userLogin(UserLoginForm userLoginForm) {
        //获取用户登录信息
        String userPhone = userLoginForm.getUserPhone();
        String userPassword = userLoginForm.getUserPassword();
        UserRegisterDO userRegisterDO = userMapper.getUserByPhone(userPhone);
        UserLoginVO userLoginVO;
        //判断用户是否存在
        if (userRegisterDO == null) {
            throw new RuntimeException("登录失败：用户不存在");
        }
        // userRegisterDO 转 UserLoginDO
        UserLoginDO userLoginDO = ConvertUnit.DOToDO(userRegisterDO, UserLoginDO.class);
        //验证密码
        if (userLoginDO.getUserPassword().equals(userPassword)) {
            //登录
            StpUtil.login(userLoginDO.getUserId());
            //保存用户信息到session
            StpUtil.getSession().set("user", userLoginDO);
            //返回用户信息
            userLoginVO = ConvertUnit.DOToVO(userLoginDO, UserLoginVO.class);
            return userLoginVO;
        }
        throw new RuntimeException("登录失败：密码错误");
    }


    /*
     * 当前用户查询
     *
     * @return GetUserVO
     * */
    @Override
    public GetUserVO getCurrentUser() {
        //从session中获取用户信息
        Object sessionUser = StpUtil.getSession().get("user");
        //将用户信息转换为UserDO类
        GetUserDO getUserDO = ConvertUnit.DOToDO(sessionUser, GetUserDO.class);
        //DO转VO
        GetUserVO userlist = ConvertUnit.DOToDO(getUserDO, GetUserVO.class);
        return userlist;
    }

    /*
     * 用户查询（根据手机号）
     * 用于登录检验是否存在该账号、
     *
     * @param getUserByPhoneForm
     * @return GetUserByPhoneVO
     * */
    @Override
    public GetUserByPhoneVO getUserByPhone(GetUserByPhoneForm getUserByPhoneForm) {
        //获取登录所用的手机号
        String userPhone = getUserByPhoneForm.getUserPhone();
        //查询该手机号的用户
        UserRegisterDO getUserDO = userMapper.getUserByPhone(userPhone);
        GetUserByPhoneVO getUserByPhoneVO = ConvertUnit.DOToVO(getUserDO, GetUserByPhoneVO.class);
        return getUserByPhoneVO;
    }

    /*
     * 用户注销
     *
     * @return void
     * */
    @Override
    public void logout() {
        //检查用户是否登录
        if (!StpUtil.isLogin()) {
            throw new RuntimeException("用户注销失败：用户未登录");
        }
        StpUtil.logout();
    }
}
